package com.ruoyi.project.system.detail.controller;

import java.util.List;

import com.ruoyi.project.system.main.domain.ScoreMain;
import com.ruoyi.project.system.main.service.IScoreMainService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.detail.domain.ScoreDetail;
import com.ruoyi.project.system.detail.service.IScoreDetailService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 得分明细Controller
 * 
 * @author ruoyi
 * @date 2021-02-22
 */
@Controller
@RequestMapping("/system/detail")
public class ScoreDetailController extends BaseController
{
    private String prefix = "system/detail";

    @Autowired
    private IScoreDetailService scoreDetailService;
    @Autowired
    private IScoreMainService scoreMainService;

    @RequiresPermissions("system:detail:view")
    @GetMapping()
    public String detail(String id,ModelMap modelMap)
    {

        modelMap.put("id",id);
        return prefix + "/detail";
    }

    /**
     * 查询得分明细列表
     */
    @RequiresPermissions("system:detail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ScoreDetail scoreDetail)
    {
        startPage();
        List<ScoreDetail> list = scoreDetailService.selectScoreDetailList(scoreDetail);
        return getDataTable(list);
    }

    /**
     * 查询得分明细列表
     */
    @RequiresPermissions("system:detail:list")
    @PostMapping("/listByScoreMainId/{mainId}")
    @ResponseBody
    public TableDataInfo listByScoreMainId(@PathVariable("mainId") Long mainId,ScoreDetail scoreDetail)
    {
        startPage();
        scoreDetail.setScoreMainId(mainId);
        List<ScoreDetail> list = scoreDetailService.selectScoreDetailList(scoreDetail);
        return getDataTable(list);
    }

    /**
     * 导出得分明细列表
     */
    @RequiresPermissions("system:detail:export")
    @Log(title = "得分明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ScoreDetail scoreDetail)
    {
        List<ScoreDetail> list = scoreDetailService.selectScoreDetailList(scoreDetail);
        ExcelUtil<ScoreDetail> util = new ExcelUtil<ScoreDetail>(ScoreDetail.class);
        return util.exportExcel(list, "detail");
    }

    /**
     * 新增得分明细
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存得分明细
     */
    @RequiresPermissions("system:detail:add")
    @Log(title = "得分明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ScoreDetail scoreDetail)
    {
        return toAjax(scoreDetailService.insertScoreDetail(scoreDetail));
    }

    /**
     * 修改得分明细
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ScoreDetail scoreDetail = scoreDetailService.selectScoreDetailById(id);
        mmap.put("scoreDetail", scoreDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存得分明细
     */
    @RequiresPermissions("system:detail:edit")
    @Log(title = "得分明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ScoreDetail scoreDetail,String score2)
    {
        //更新主表的总分
        //score2
        Long score = scoreDetail.getScore();
        //对这更改前后的积分进行比较
        Long aLongScore2 = Long.valueOf(score2);
        ScoreMain scoreMain = new ScoreMain();
        scoreMain.setId(scoreDetail.getScoreMainId());
        List<ScoreMain> list = scoreMainService.selectScoreMainAll(scoreMain);
        if(null != list && list.size()>0){
            Long userId = list.get(0).getUserId();
            if(aLongScore2>score){//说明分数降低了
                long l = aLongScore2 - score;
                scoreMainService.updateScoreMainTotalScoreMinus(userId,l);
            }else if(aLongScore2<score){
                //说明分数升高了
                long ll = score - aLongScore2;
                scoreMainService.updateScoreMainTotalScore(userId,ll);
            }
        }
        return toAjax(scoreDetailService.updateScoreDetail(scoreDetail));
    }

    /**
     * 删除得分明细
     */
    @RequiresPermissions("system:detail:remove")
    @Log(title = "得分明细", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        ScoreDetail scoreDetail = scoreDetailService.selectScoreDetailById(Long.valueOf(ids));
        //需要去更新主表的分数
        ScoreMain scoreMain = new ScoreMain();
        scoreMain.setId(scoreDetail.getScoreMainId());
        List<ScoreMain> list = scoreMainService.selectScoreMainAll(scoreMain);
        if(null != list && list.size()>0){
            Long userId = list.get(0).getUserId();
            Long score = scoreDetail.getScore();
            scoreMainService.updateScoreMainTotalScoreMinus(userId,score);
        }
        return toAjax(scoreDetailService.deleteScoreDetailByIds(ids));
    }
}
